﻿using MediatR;
using Schedulingclasses.Api.User.Command;
using Schedulingclasses.Domain;
using Schedulingclasses.ErrorCode;
using Schedulingclasses.Infrastructure;
using System.Drawing;

namespace Schedulingclasses.Api.User.Handler
{
    public class CreateInstncceHandler : IRequestHandler<CreateInstncceComandd, ApiResult<int>>
    {
        private readonly IBaseRepository<ClassInfo> classinfo;//排课信息
        private readonly IBaseRepository<BanjiInfo> banjiinfo;//班级
        private readonly IBaseRepository<Branchschool> branchschool;//分校
        private readonly IBaseRepository<GradeInfo> graeinfo;//年级
        private readonly IBaseRepository<ProcessInfo> processinfo;//节点
        private readonly IBaseRepository<instancce> instancce;//流程
        private readonly ILogger<CreateInstncceHandler> logger;

        public CreateInstncceHandler(IBaseRepository<ClassInfo> classinfo,IBaseRepository<BanjiInfo> banjiinfo,IBaseRepository<Branchschool> branchschool,IBaseRepository<GradeInfo> graeinfo,IBaseRepository<ProcessInfo> processinfo,IBaseRepository<instancce> instancce,ILogger<CreateInstncceHandler> logger)
        {
            this.classinfo = classinfo;
            this.banjiinfo = banjiinfo;
            this.branchschool = branchschool;
            this.graeinfo = graeinfo;
            this.processinfo = processinfo;
            this.instancce = instancce;
            this.logger = logger;
        }

        /// <summary>
        /// 添加流程信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiResult<int>> Handle(CreateInstncceComandd request, CancellationToken cancellationToken)
        {
            try
            {

                logger.LogInformation("成功完成提交操作");
                var res=new ApiResult<int>();

                var classinfos = await classinfo.GetValue(x => x.ClassId.Equals(request.ClassId));
                var banjiinfos=await banjiinfo.GetValue(x=>x.BanjiId.Equals(classinfos.ClassName));
                var branchschools = await branchschool.GetValue(x => x.BranchschoolId.Equals(classinfos.BranchschoolId));
                var graeinfos = await graeinfo.GetValue(x => x.GradeId.Equals(classinfos.GradeId));
                var processinfos = await processinfo.GetValue(x => x.ClassId.Equals(classinfos.ClassId));
                var processinfose = await processinfo.GetValue(x => x.ClassId.Equals(request.ClassId) && x.Nodenumber.Equals(processinfos.Lowernode));


                //添加流程信息
                var model = new instancce()
                {
                    ClassId=request.ClassId,
                    BranchschoolName= branchschools.BranchschoolName,
                    GradeName= graeinfos.GradeName,
                    BanjiName= banjiinfos.BanjiName,
                    StatusId=1,
                    Status="待审核",
                    Reason=request.Reason,
                    ApplicantId=request.UserId,
                    ApplicantName=request.Username,
                    Nodenumber= processinfos.Nodenumber,
                    Nodename= processinfos.Nodename,
                    UserId= processinfos.UserId,
                    UserName= processinfos.Username,
                    Lowernode= processinfose.Nodenumber,
                    Lowernodename= processinfose.Nodename,
                    LoUserId= processinfose.UserId,
                    LoUserName= processinfose.Username,
                };

                var info = await instancce.AddAync(model);

                res.msg = "提交成功";
                res.code = ApiREunm.OK;
                return res;

            }
            catch (Exception ex)
            {

                logger.LogError("=>" + ex.Message);
                throw ex;
            }
        }
    }
}
